#include <string.h>
#include <stdio.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/stat.h>
#include "coro.h"
#include "fs_walk.h"

void find_dir(char *path)
{
	char new_path[512];
    
	DIR *dir = opendir(path);
	struct dirent *entry;

    entry = readdir(dir);

	while(entry = readdir(dir)) {
		if(strcmp(entry->d_name, ".") == 0)
			continue;
		if(strcmp(entry->d_name, "..") == 0)
			continue;
        
		bzero(new_path, sizeof(new_path));
		sprintf(new_path, "%s/%s", path, entry->d_name);

		if(entry->d_type == DT_DIR) 
			find_dir(new_path);
		if(entry->d_type == DT_REG)
			coro_yield((unsigned long)new_path);
	}
	closedir(dir);
}

void fs_walk()
{
	find_dir(dir_path);
	coro_yield((unsigned long)NULL);
}
